package com.lbn.monitor.app.history;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lbn.monitor.R;
import com.lbn.monitor.api.model.RealTimeValue;
import com.lbn.monitor.base.AppBaseFragment;
import com.lbn.monitor.util.DataUtils;
import com.lbn.monitor.util.String2Num;
import com.lbn.monitor.widget.calendar.TimeUtils;
import com.linheimx.app.library.adapter.DefaultValueAdapter;
import com.linheimx.app.library.adapter.IValueAdapter;
import com.linheimx.app.library.charts.LineChart;
import com.linheimx.app.library.data.Entry;
import com.linheimx.app.library.data.Line;
import com.linheimx.app.library.data.Lines;
import com.linheimx.app.library.model.HighLight;
import com.linheimx.app.library.model.WarnLine;
import com.linheimx.app.library.model.XAxis;
import com.linheimx.app.library.model.YAxis;
import com.linheimx.app.library.touch.TouchListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ChartFragment extends AppBaseFragment<ChartPresenter> implements
        IChartView<List<RealTimeValue>>,TouchListener.OnLoadData {


    @BindView(R.id.chart)
    LineChart chart;
    @BindView(R.id.device)
    TextView device;
    private HistoryActivity act;
    private Map<Integer,String>dateData;
    public static ChartFragment newInstance() {
        Bundle args = new Bundle();
        ChartFragment fragment = new ChartFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_chart, null, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void onPresenterCreated() {
        super.onPresenterCreated();

        dateData = new HashMap<>();

        act = (HistoryActivity) getActivity();

        XAxis xAxis = chart.get_XAxis();
        xAxis.set_ValueAdapter(new IValueAdapter() {
            @Override
            public String value2String(double value) {

                int i = (int) Math.round(value);
                int size = dateData.size()-1;

                if(i<0){
                    return TimeUtils.cutDay(dateData.get(0));
                }else if(i>size){
                    return TimeUtils.cutDay(dateData.get(size));
                }
                return TimeUtils.cutDay(dateData.get(i));
            }
        });

        HighLight highLight = chart.get_HighLight();
        highLight.setEnable(true);// 启用高亮显示  默认为启用状态
        highLight.setxValueAdapter(new IValueAdapter() {
            @Override
            public String value2String(double value) {
                int i = (int) Math.round(value);
                return "X:" + dateData.get(i);
            }
        });
        highLight.setyValueAdapter(new IValueAdapter() {
            @Override
            public String value2String(double value) {
                return "Y:" + value;
            }
        });


        YAxis yAxis = chart.get_YAxis();
        yAxis.set_ValueAdapter(new DefaultValueAdapter(2));

        Line line = new Line();
        Lines lines = new Lines();
        lines.addLine(line);
        chart.setLines(lines);
        chart.setOnLoadData(this);
//        List<WarnLine> xWarns = new ArrayList<>();
//        xWarns.add(new WarnLine(3.5));
//        xAxis.setListWarnLins(xWarns);
//        xAxis.
    }

    @Override
    protected ChartPresenter createPresenter() {
        return new ChartPresenter();
    }

    @Override
    public void refreshComplete(List<RealTimeValue> data) {
        chart.clearData();
        dateData.clear();
        if(dateData == null){
            dateData = new HashMap<>();
        }
        Line line = new Line();
        Lines lines = new Lines();
        lines.addLine(line);
        chart.setLines(lines);

        translateData(data);
        chart.notifyDataChanged();
        chart.invalidate();
        hideLoadingView();
    }

    @Override
    public void addMoreData(List<RealTimeValue> data, boolean isMore) {
        translateData(data);
        chart.notifyDataChanged();
        chart.invalidate();
        hideLoadingView();
    }

    @Override
    public void onLoadData() {
        act.onLoadMore();
    }


    public void translateData(List<RealTimeValue> datas){

        int id = act.getPollutantId();

        Line line = chart.getlines().getLines().get(0);
        List<WarnLine>warns = new ArrayList<>();
        warns.add(new WarnLine(String2Num.str2Double(act.getStandardvalue())));
//        warns.add(new WarnLine(33d));
        chart.get_YAxis().setListWarnLins(warns);

        for(RealTimeValue data : datas){

            String value = DataUtils.getAvg(id,data);

            int pos;
            if(line.getEntries()!=null){
                pos = line.getEntries().size();
            }else{
                pos = 0;
            }

            line.addEntry(new Entry(pos,String2Num.str2Double(value)));
            dateData.put(pos,data.getDataTime());
        }
    }

    public void setDevice(String device) {
        this.device.setText(device);
    }
}
